var mongoose = require('mongoose');

Schema = mongoose.Schema;

// Create a schema for our data
var usersSchema = new Schema({
    userName:{type:String,unique: true,required: true},
    firstName:String,
    lastName:String,
    password: String,
    contactNo: String,
    emailId:String
});
var userModel = mongoose.model('Users', usersSchema);
console.log("Created Users model");

exports.getUser = function (request, response) {
	var username = request.body.username;
	userModel.find({'userName':username}, function(err, users) {
		console.log("Users",users);
		console.log(users.length);
		if(users === 'undefined' || users.length ==0){
			response.send("Error:User name is not Exist");
			console.log("Error:User name is not Exist");
		}else{
			response.send("Succes");
		}
        /*var newTestUser = new userModel({
           userName:"vel123"
        });
        newTestUser.save(function (err,user){
            if(err) return console.error(err);
            console.dir(user);
        })*/
});};

exports.createUser = function (request, response) {
    console.log(request.body);
	var userDetails = new userModel({
			userName : request.body.userName,
			firstName : request.body.firstName,
			lastName : request.body.lastName,
			password : request.body.password,
			mobileNo : request.body.mobileNo,
			emailId : request.body.emailId
	});
	console.log("User Details : ",userDetails);
    userDetails.save(function(err, user) {
		console.log("Users",user);
		if(user === 'undefined'){
			var errorData = 'Unable to add user details';
		}else{
			//response.send("Succes");
            response.send(user);
		}
});};
